package net.ksic.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class JSONParser {
	
	public List<HashMap<String,Object>> parse(JSONObject jObject){
		 
        JSONArray jActitem = null;
        try {
            // Retrieves all the elements in the 'actitem' array
            jActitem = jObject.getJSONArray("actitem");
        } catch (JSONException e) {
            e.printStackTrace();
        }
 
        // Invoking getactitem with the array of json object
        // where each json object represent a Actitem
        return getactitem(jActitem);
    }
 
    private List<HashMap<String, Object>> getactitem(JSONArray jActitem){
        int actitemCount = jActitem.length();
        List<HashMap<String, Object>> actitemList = new ArrayList<HashMap<String,Object>>();
        HashMap<String, Object> actitem = null;
 
        // Taking each Actitem, parses and adds to list object
        for(int i=0; i<actitemCount;i++){
            try {
                // Call getActitem with Actitem JSON object to parse the Actitem
                actitem = getActitem((JSONObject)jActitem.get(i));
                actitemList.add(actitem);
 
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
 
        return actitemList;
    }
 
    // Parsing the Actitem JSON object
    private HashMap<String, Object> getActitem(JSONObject jActitem){
 
        HashMap<String, Object> Actitem = new HashMap<String, Object>();
        /*String ActitemName = "";
        String flag="";
        String language = "";
        String capital = "";
        String currencyCode = "";
        String currencyName = "";
 
        try {
            ActitemName = jActitem.getString("Actitemname");
            flag = jActitem.getString("flag");
            language = jActitem.getString("language");
            capital = jActitem.getString("capital");
            currencyCode = jActitem.getJSONObject("currency").getString("code");
            currencyName = jActitem.getJSONObject("currency").getString("currencyname");
 
            String details =        "Language : " + language + "\n" +
                                "Capital : " + capital + "\n" +
                                "Currency : " + currencyName + "(" + currencyCode + ")";
 
            Actitem.put("Actitem", ActitemName);
            Actitem.put("flag", R.drawable.blank);
            Actitem.put("flag_path", flag);
            Actitem.put("details", details);
 
        } catch (JSONException e) {
            e.printStackTrace();
        }*/
        return Actitem;
    }

}